package com.xique.park.command.handler;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.api.IParkIpcCommandApi;
import com.xique.park.bean.domain.ParkChargeCarTemp;
import com.xique.park.bean.domain.ParkDeviceNew;
import com.xique.park.command.handler.base.BaseCommand;
import com.xique.park.constant.CommandConstant;
import com.xique.park.constant.ParkApiName;
import com.xique.park.service.service.IParkChargeCarTempService;
import com.xique.park.service.service.IParkDeviceNewService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 设置车辆管理类型的计费价格
 *
 * @author WLD
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class SetChargeCommandHandler extends BaseCommand {

    private final IParkDeviceNewService parkDeviceNewService;

    private final IParkChargeCarTempService chargeCarTempService;

    private final IParkIpcCommandApi parkIpcCommandApi;

    @Override
    public String getServiceName() {
        return ParkApiName.COMMAND_SET_MNGTYPE_CHARGE;
    }

    @Override
    public String getVersion() {
        return ParkApiName.VER_2_0;
    }

    @Override
    public AjaxResult issue(Long parkId, Long laneId, String mngTypeIds, String sn,
                            MultipartFile file, String fileName, String cmd) {
        checkParkId(parkId);
        checkMngTypeIds(mngTypeIds);
        checkSn(sn);

        // 查询车场下的所有设备
        ParkDeviceNew parkDeviceNew = new ParkDeviceNew();
        parkDeviceNew.setParkId(parkId);
        parkDeviceNew.setStatus("normal");
        parkDeviceNew.setIsMain("Y");
        List<ParkDeviceNew> deviceList = parkDeviceNewService.selectParkDeviceNewList(parkDeviceNew);

        if (CollUtil.isNotEmpty(deviceList)) {
            // 查询车辆管理类型列表
            String[] mngTypeIdArray = mngTypeIds.split(",");
            List<ParkChargeCarTemp> chargeCarTempList = new ArrayList<>(16);
            ParkChargeCarTemp parkChargeCarTemp = new ParkChargeCarTemp();
            // 查询选中的车辆管理类型的所有费率
            for (String mngTypeId : mngTypeIdArray) {
                parkChargeCarTemp.setMngTypeId(Long.parseLong(mngTypeId));
                chargeCarTempList.addAll(chargeCarTempService.selectParkChargeCarTempList(parkChargeCarTemp));
            }
            Map<String, Object> resultMap = new HashMap<>(deviceList.size());
            for (ParkDeviceNew deviceNew : deviceList) {
                Object result = downMngTypeTempCharge(parkId, deviceNew.getCode(), chargeCarTempList, cmd);
                resultMap.put(deviceNew.getCode(), result);
            }
            return AjaxResult.success(resultMap);
        } else {
            return AjaxResult.success("车场下没有设备，无需下发");
        }
    }

    private AjaxResult downMngTypeTempCharge(Long parkId, String sn, List<ParkChargeCarTemp> chargeCarTempList, String cmd) {
        JSONObject baseObj = getBaseObj(sn, getServiceName(), getVersion());
        JSONObject dataObj = new JSONObject();
        dataObj.put(CommandConstant.LIST, chargeCarTempList);
        List<Long> recIdList = chargeCarTempList.stream().map(ParkChargeCarTemp::getId).collect(Collectors.toList());
        dataObj.put(CommandConstant.REC_IDS, recIdList);
        baseObj.put(CommandConstant.DATA, dataObj);

        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSON.toJavaObject(baseObj, Map.class);

        AjaxResult issueResult = parkIpcCommandApi.issue(params);

        log.info("[停车系统]设置车辆管理类型的计费价格命令下发，服务名：{}，版本号：{}，车场ID：{}，设备编号：{}，下发参数：{}，下发结果：{}",
                getServiceName(), getVersion(), parkId, sn, params, issueResult);

        updateCommonInfo(cmd, params, issueResult);

        return issueResult;
    }

}
